#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <easyx.h>//引用图形库头文件
int main()
{
	float width,height;
	width = 600;//宽
	height = 400;//长
	initgraph(width,height);//建一个背景画布

	float ball_x,ball_y,ball_vy,radius,gravity;//半径
	radius = 10;
	ball_x = width/4;
	ball_y = height - radius;//初始在下端
	ball_vy = 0;
	gravity = 0.7;/*重力*/

	float rect_left_x,rect_top_y,rect_width,rect_height;//定义矩形长宽角
    float rect_vx;//定义矩形速度
	float rect_left_x2,rect_top_y2,rect_width2,rect_height2;//定义矩形2长宽角
    float rect_vx2;//定义矩形2速度

	rect_width = 20;
	rect_height = 100;
	rect_left_x = width*3/4;
	rect_top_y = height - rect_height;
	rect_vx = -3;

	rect_width2 = 20;
	rect_height2 = 100;
	rect_left_x2 = width*3/4;
	rect_top_y2 = 0;
	rect_vx2 = -3;


	int score = 0;//最开始为0分


	while (1)
	{
		if (kbhit())//当按时
		{
			char input = _getch();
			if(input==' ')
			{
             ball_vy = -12;//按空格换速度方向
			}
		}
		ball_vy = ball_vy + gravity;
		ball_y = ball_y + ball_vy;
		if(ball_y>=height - radius)//当小球落地时
		{
        ball_y = 0;
		ball_y = height - radius;
		}
		rect_left_x=rect_left_x + rect_vx;
		rect_left_x2=rect_left_x2 + rect_vx;
		if(rect_left_x<=0)
		{
			score = score + 1;//得分加①
			rect_left_x = width;//重新出现在右侧
			rect_left_x2 = width;
			rect_height = rand() % int(height/6) + height/6; //矩形随机高度
			rect_vx = rand()/float(RAND_MAX) *4 - 7;//矩形随机速度
			rect_height2 = rand() % int(height/4) + height/4; //矩形2随机高度
		}

		if(((rect_left_x<=ball_x+radius) || (rect_left_x2<=ball_x+radius/*小球右与矩形1和2左接触*/)) 
			&& ((rect_left_x+rect_width>=ball_x-radius) || (rect_left_x2+rect_width2>=ball_x-radius/*小球左与矩形1和2右接触*/))
			&& (height-rect_height<=ball_y+radius/*小球下面与矩形上面接触*/))
		{
			score = 0;//碰到得分为零
			Sleep(100);
		}//接触减速

		cleardevice();//清空
	fillrectangle(rect_left_x,height - rect_height,rect_width+rect_left_x,height);//填充一个长方形
	fillrectangle(rect_left_x2, rect_height2, rect_left_x2 + rect_width2,0);//画长方形2
    fillcircle(ball_x,ball_y,radius);//画球

	TCHAR s[20];//定义字符串
	_stprintf(s,_T("%d"), score);//转换为字符串
	settextstyle(40, 0, _T("宋体"));//定文子大小和字体
	outtextxy(50, 30, s);//输出得分

	Sleep(10); //停10毫秒
	}
	

	_getch();

	return 0;
}